//
//  SlideTabBarView.m
//  SlideTabBar
//
//  Created by Mr.LuDashi on 15/6/25.
//  Copyright (c) 2015年 李泽鲁. All rights reserved.
//

#import "SlideTabBarView.h"
#import "YYSRecommendController.h"
#import "YYSTourismController.h"
#import "YYSMovieController.h"
#import "YYSVarietyController.h"
#import "YYSMusicController.h"
#import "YYSOnlineController.h"
#define TOPHEIGHT 55
@interface SlideTabBarView()<UIScrollViewDelegate>
///@brife 整个视图的大小
@property (assign) CGRect mViewFrame;

///@brife 下方的ScrollView
@property (strong, nonatomic) UIScrollView *scrollView;

///@brife 上方的按钮数组
@property (strong, nonatomic) NSMutableArray *topViews;

///@brife 下方的表格数组
@property (strong, nonatomic) NSMutableArray *scrollTableViews;

///@brife TableViews的数据源
@property (strong, nonatomic) NSMutableArray *dataSource;

///@brife 当前选中页数
@property (assign) NSInteger currentPage;

///@brife 下面滑动的View
@property (strong, nonatomic) UIView *slideView;

///@brife 上方的view
@property (strong, nonatomic) UIView *topMainView;

///@brife 上方的ScrollView
@property (strong, nonatomic) UIScrollView *topScrollView;

@end

@implementation SlideTabBarView

-(instancetype)initWithFrame:(CGRect)frame WithCount: (NSInteger) count{
    self = [super initWithFrame:frame];
    
    if (self) {
        _mViewFrame = frame;
        _tabCount = count;
        _topViews = [[NSMutableArray alloc] init];
        _scrollTableViews = [[NSMutableArray alloc] init];
        
        [self initDataSource];
        
        [self initScrollView];
        
        [self initTopTabs];
        
        [self initDownTables];
        
        [self initDataSource];
        
        [self initSlideView];
        
    }
    
    return self;
}


#pragma mark -- 初始化滑动的指示View
-(void) initSlideView{
    
    CGFloat width = _mViewFrame.size.width / 6;
    
    if(self.tabCount <=6){
        width = _mViewFrame.size.width / self.tabCount;
    }

    _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, TOPHEIGHT - 5, width, 5)];
    [_slideView setBackgroundColor:YYSGlobalColor];
    [_topScrollView addSubview:_slideView];
}


#pragma mark -- 初始化表格的数据源
-(void) initDataSource{
    _dataSource = [[NSMutableArray alloc] initWithCapacity:_tabCount];
    
    for (int i = 1; i <= _tabCount; i ++) {
        
        NSMutableArray *tempArray  = [[NSMutableArray alloc] initWithCapacity:20];
        
        for (int j = 1; j <= 20; j ++) {
            
            NSString *tempStr = [NSString stringWithFormat:@"我是第%d个TableView的第%d条数据。", i, j];
            [tempArray addObject:tempStr];
        }
        
        [_dataSource addObject:tempArray];
    }
}


#pragma mark -- 实例化ScrollView
-(void) initScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _mViewFrame.origin.y, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT)];
    _scrollView.contentSize = CGSizeMake(_mViewFrame.size.width * _tabCount, _mViewFrame.size.height - 60);
    _scrollView.backgroundColor = [UIColor whiteColor];
    
    _scrollView.pagingEnabled = YES;
    
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
}



#pragma mark -- 实例化顶部的tab

-(void) initTopTabs{
    CGFloat width = _mViewFrame.size.width / 6;
    NSArray *name1 = @[@"推荐",@"电影",@"综艺",@"音乐",@"直播",@"旅游"];
    NSArray *name2 = @[@"संस्तुति",@"चलचित्र",@"कला",@"संगीत",@"लाइव",@"दौरा"];
    if(self.tabCount <=6){
        width = _mViewFrame.size.width / self.tabCount;
    }
    
    _topMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)];
    
    _topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)];
    
    _topScrollView.showsHorizontalScrollIndicator = NO;
    
    _topScrollView.showsVerticalScrollIndicator = YES;
    
    _topScrollView.bounces = NO;
    
    _topScrollView.delegate = self;
    
    if (_tabCount >= 6) {
        _topScrollView.contentSize = CGSizeMake(width * _tabCount, TOPHEIGHT);

    } else {
        _topScrollView.contentSize = CGSizeMake(_mViewFrame.size.width, TOPHEIGHT);
    }
    
    
    [self addSubview:_topMainView];
    
    [_topMainView addSubview:_topScrollView];
    
    
    
    for (int i = 0; i < _tabCount; i ++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TOPHEIGHT)];
        
        view.backgroundColor = YYSRGBColor(243, 243, 243);
        view.tag = i;
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 35)];
        label1.text = name1[i];
        label1.textColor = YYSRGBColor(85, 85, 85);
        label1.textAlignment = NSTextAlignmentCenter;
        label1.font = [UIFont systemFontOfSize:17];
        [view addSubview:label1];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, width, 25)];
        label2.text = name2[i];
        label2.textColor = YYSRGBColor(85, 85, 85);
        label2.font = [UIFont systemFontOfSize:15];
        label2.textAlignment = NSTextAlignmentCenter;
        [view addSubview:label2];

        if (i == 0) {
            label1.textColor = YYSGlobalColor;
            label2.textColor = YYSGlobalColor;
        }
        

        
        [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabButton:)]];
        [_topViews addObject:view];
        [_topScrollView addSubview:view];
    }
}



#pragma mark --点击顶部的按钮所触发的方法
-(void) tabButton: (UITapGestureRecognizer *) tap{
    UIView *view = tap.view;
    [_scrollView setContentOffset:CGPointMake(view.tag * _mViewFrame.size.width, 0) animated:YES];
}

#pragma mark --初始化下方的TableViews
YYSRecommendController *recommend;
YYSMovieController *movie;
YYSVarietyController *variety;
YYSMusicController *music;
YYSRecommendController *recommend2;
YYSTourismController *tourism;
-(void) initDownTables{
    
    for (int i = 0; i < 6; i ++) {
        
        
        if (i == 0) {
            
            recommend = [[YYSRecommendController alloc] init];
            [self.viewController addChildViewController:recommend];
            recommend.view.yy_width = YYSScreenW;
            recommend.view.frame = CGRectMake(YYSScreenW*i, 0, YYSScreenW, YYSScreenH);
            [_scrollTableViews addObject:recommend.view];
            [_scrollView addSubview:recommend.view];

            
        } else if (i == 1) {
            movie = [[YYSMovieController alloc] init];
            [self.viewController addChildViewController:movie];
            movie.view.yy_width = YYSScreenW;
            movie.view.frame = CGRectMake(YYSScreenW*i, 0, YYSScreenW, YYSScreenH);
            [_scrollTableViews addObject:movie.view];
            [_scrollView addSubview:movie.view];
            
        }else if (i == 2) {
            variety = [[YYSVarietyController alloc] init];
            [self.viewController addChildViewController:variety];
            variety.view.yy_width = YYSScreenW;
            variety.view.frame = CGRectMake(YYSScreenW*i, 0, YYSScreenW, YYSScreenH);
            [_scrollTableViews addObject:variety.view];
            [_scrollView addSubview:variety.view];
            
        }else if (i == 3) {
            music = [[YYSMusicController alloc] init];
            [self.viewController addChildViewController:music];
            music.view.yy_width = YYSScreenW;
            music.view.frame = CGRectMake(YYSScreenW*i, 0, YYSScreenW, YYSScreenH);
            [_scrollTableViews addObject:music.view];
            [_scrollView addSubview:music.view];
            
        }else if (i == 4) {
//            YYSOnlineController *tourism = [[YYSOnlineController alloc] init];
//            [self.viewController addChildViewController:tourism];
//            tourism.view.yy_width = YYSScreenW;
//            tourism.view.frame = CGRectMake(YYSScreenW*i, 0, YYSScreenW, YYSScreenH);
//            [_scrollTableViews addObject:tourism.view];
//            [_scrollView addSubview:tourism.view];
            recommend2 = [[YYSRecommendController alloc] init];
            [self.viewController addChildViewController:recommend2];
            recommend2.view.yy_width = YYSScreenW;
            recommend2.view.frame = CGRectMake(YYSScreenW*i, 0, YYSScreenW, YYSScreenH);
            [_scrollTableViews addObject:recommend2.view];
            [_scrollView addSubview:recommend2.view];

        } else {
            tourism = [[YYSTourismController alloc] init];
            [self.viewController addChildViewController:tourism];
            tourism.view.yy_width = YYSScreenW;
            tourism.view.frame = CGRectMake(YYSScreenW*i, 0, YYSScreenW, YYSScreenH);
            [_scrollTableViews addObject:tourism.view];
            [_scrollView addSubview:tourism.view];
            
        }
        
        
    }
    
}


#pragma mark --根据scrollView的滚动位置复用tableView，减少内存开支
-(void) updateTableWithPageNumber: (NSUInteger) pageNumber{
    
    [self changeBackColorWithPage:pageNumber];
    
    int tabviewTag = pageNumber % 6;
    
    CGRect tableNewFrame = CGRectMake(pageNumber * _mViewFrame.size.width, 0, _mViewFrame.size.width, _mViewFrame.size.height);
    
    UIView *view = _scrollTableViews[tabviewTag];
    NSLog(@"%@",_scrollTableViews[tabviewTag]);
    view.frame = tableNewFrame;
    [recommend.tableView reloadData];
    [movie.tableView reloadData];
    [variety.tableView reloadData];
    [music.tableView reloadData];
    [tourism.tableView reloadData];
//    [view. reloadData];
}


- (void) changeBackColorWithPage: (NSInteger) currentPage {
    for (int i = 0; i < _topViews.count; i ++) {
        UIView *tempView = _topViews[i];
        for (UILabel *label in [tempView subviews]) {
            if (i == currentPage) {
                label.textColor = YYSGlobalColor;
            } else {
                label.textColor = YYSRGBColor(85, 85, 85);
            }
        }
        
    }
}


#pragma mark -- scrollView的代理方法

-(void) modifyTopScrollViewPositiong: (UIScrollView *) scrollView{
    if ([_topScrollView isEqual:scrollView]) {
        CGFloat contentOffsetX = _topScrollView.contentOffset.x;
        
        CGFloat width = _slideView.frame.size.width;
        
        int count = (int)contentOffsetX/(int)width;
        
        CGFloat step = (int)contentOffsetX%(int)width;
        
        CGFloat sumStep = width * count;
        
        if (step > width/2) {
            
            sumStep = width * (count + 1);
            
        }
        
        [_topScrollView setContentOffset:CGPointMake(sumStep, 0) animated:YES];
        return;
    }

}

///拖拽后调用的方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //[self modifyTopScrollViewPositiong:scrollView];
}



-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self scrollViewDidEndDecelerating:scrollView];


}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{
    if ([scrollView isEqual:_scrollView]) {
        _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width;
        
        _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width;
        
        //    UITableView *currentTable = _scrollTableViews[_currentPage];
        //    [currentTable reloadData];
        
        [self updateTableWithPageNumber:_currentPage];

        return;
    }
    [self modifyTopScrollViewPositiong:scrollView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([_scrollView isEqual:scrollView]) {
        CGRect frame = _slideView.frame;
        
        if (self.tabCount <= 6) {
             frame.origin.x = scrollView.contentOffset.x/_tabCount;
        } else {
             frame.origin.x = scrollView.contentOffset.x/6;
            
        }
        
       
        _slideView.frame = frame;
    }
    
}






@end
